package it.av.training.rest.service;

import static org.apache.commons.lang3.StringUtils.isBlank;
import it.av.training.rest.exception.ErrorCode;
import it.av.training.rest.exception.RestException;
import it.av.training.rest.models.Role;
import it.av.training.rest.models.User;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * 
 * @author alessandro vincelli
 */
public class UserServiceImpl implements UserService {

    private static Logger logger = Logger.getLogger(UserServiceImpl.class);

    /**
     * {@inheritDoc}
     */
    @Override
    public User addUser(User user) {
        if (users.containsKey(user.getId())) {
            throw new RestException("user already exist", ErrorCode.ERRORE_GENERICO, getClass().getName(), Level.ERROR);
        }
        user.setId(UUID.randomUUID().toString());
        users.put(user.getId(), user);
        logger.info("user added " + ReflectionToStringBuilder.toString(user));
        return user;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void removeUser(String userID) {
        if (isBlank(userID)) {
            throw new RestException("blank userID", ErrorCode.ERRORE_GENERICO, getClass().getName(), Level.ERROR);
        }
        if (users.containsKey(userID)) {
            users.remove(users.get(userID));
            logger.info("user removed " + userID);
        } else {
            throw new RestException("user non found", ErrorCode.ERRORE_GENERICO, getClass().getName(), Level.ERROR);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public User editUser(User user) {
        if (users.containsKey(user.getId())) {
            logger.info("user modified " + ReflectionToStringBuilder.toString(user));
            return users.put(user.getId(), user);
        } else {
            throw new RestException("user non found", ErrorCode.ERRORE_GENERICO, getClass().getName(), Level.ERROR);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public User getUser(String userID) {
        if (isBlank(userID)) {
            throw new RestException("blank userID", ErrorCode.ERRORE_GENERICO, getClass().getName(), Level.ERROR);
        }
        logger.info("user returned " + ReflectionToStringBuilder.toString(users.get(userID)));
        return users.get(userID);
    }

    /**
     * {@inheritDoc}
     */
    private static final Map<String, User> users;
    static {
        Map<String, User> listOfUser = new HashMap<String, User>();
        listOfUser.put("1", new User("1", Role.ROLE_USER, "Dante", "Cruciani"));
        listOfUser.put("2", new User("2", Role.ROLE_USER, "Giuseppe", "Baiocchi"));
        listOfUser.put("3", new User("3", Role.ROLE_USER, "Carmelina", "Carmelina"));
        listOfUser.put("4", new User("4", Role.ROLE_USER, "Mario", "Angeletti"));
        users = listOfUser;
    }
}
